﻿using System;
using System.Text;
using System.Windows.Forms;
using Tp.GUI.Base;
using Tp.Base;
using TpBaseObjects.Objects;
using TpData.Manager;
using TpTools;
using Tp.Properties;
using TpExtensions;

namespace Tp.GUI.Dialog {
    public partial class Angebot : TpBase {

        #region Members

        
        #endregion

        #region Constructors

        public Angebot() {
            InitializeComponent();
        }

        public Angebot(string OrgName, string suchbegriff)
            : this() {
            eventHandler.Changed += SetSelectEmpfänger;
            eventHandler.AfterSelected += SetVersand;
            Cancel = true;
            Organisation org = OrganisationDataManager.LoadOrganisation(OrgName, suchbegriff);
            if (org != null) {
                organisationBindingSource.DataSource = org;
                personBindingSource.DataSource = org.MyPersons;
            } else {
                string msg = String.Format(Resources.NameInDbNichtGefunden, OrgName, suchbegriff);
                MessageBox.Show(msg, "Dialoag Auswahl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Cancel = true;
                this.Close();
            }
        }

        #endregion

        #region Properties

        public bool Cancel {
            get;
            set;
        }

        public string SelectedType {
            get { return this.selectedType; }
            set { this.selectedType = value; }
        }

        public int Versand {
            get { return this.versand; }
            set { this.versand = value; }
        }

        public Recipient MyRecipient {
            get { return this.myRecipient; }
        }

        #endregion

        #region Events

        private void Angebot_Load(object sender, EventArgs e) {
            this.rbFrau.Checked = true;
            this.txtBetreff.Text = "Angebot";
            this.chkZuHanden.Enabled = false;
            this.rbMail.Checked = true;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e) {
            SetDataBeforeClose();
            this.Close();
        }

        private void dgEmpfänger_CellClick(object sender, DataGridViewCellEventArgs e) {
            eventHandler.ChangeSelect(sender, e);
        }

        private void dgPerson_CellClick(object sender, DataGridViewCellEventArgs e) {
            eventHandler.ChangeSelect(sender, e);
        }

        private void gbVersand_Validated(object sender, EventArgs e) {
            //eventHandler.SelectChanged(sender, e);
        }

        private void rbBrief_CheckedChanged(object sender, EventArgs e) {
            GlobalVar.Versand = (int)GlobalVar.VersandArt.Mail;
            eventHandler.SelectChanged(sender, e);
        }

        private void rbFax_CheckedChanged(object sender, EventArgs e) {
            GlobalVar.Versand = (int)GlobalVar.VersandArt.Fax;
            eventHandler.SelectChanged(sender, e);
        }

        private void rbMail_CheckedChanged(object sender, EventArgs e) {
            GlobalVar.Versand = (int)GlobalVar.VersandArt.Brief;
            eventHandler.SelectChanged(sender, e);
        }

        private void chkZuHanden_CheckedChanged(object sender, EventArgs e) {
            eventHandler.SelectChanged(sender, e);
        }

        #endregion

        #region Methods

        private void SetBetreff(string text, bool addText) {
            text = " " + text;
            string newText = this.txtBetreff.Text;
            if(addText) {
                newText += text;
            } else {
                newText = newText.Replace(text, "");
            }
            this.txtBetreff.Text = newText;
        }

        private void SetSelectEmpfänger(object sender, EventArgs e) {
            StringBuilder txt = new StringBuilder();
            DataGridView dgv = sender as DataGridView;
            DataGridViewRow row = dgv.CurrentRow;

            if (dgv.Tag.ToString() == "Org") {
                SelectedType = "Org";
                this.chkZuHanden.Checked = false;
                this.chkZuHanden.Enabled = false;
            }
            if (dgv.Tag.ToString() == "Person") {
                SelectedType = "Person";
                this.chkZuHanden.Enabled = true;
            }
            txt.Append(GetSelectedText());
            this.txtAuswahl.Text = txt.ToString();
        }

        private void SetVersand(object sender, EventArgs e) {
            StringBuilder txt = new StringBuilder();
            txt.Append(GetSelectedText());
            this.txtAuswahl.Text = txt.ToString();
        }

        private void SetRecipientData() {
            Organisation org = GlobalVar.currentOrganisation;
            Person pers = GlobalVar.currentPerson;

            if (SelectedType == "Org") {
                myRecipient.Anrede = Resources.Anrede + (rbFrau.Checked ? " " + Resources.Frau : Resources.AnredeErgänzungHerr + Resources.Herr).AddSpace() + org.OrgName;
                myRecipient.Name = org.Prefix.AddSpace() + org.OrgName.AddSpace() + org.ZusätzeFW.AddSpace() + org.RechtsForm;
                myRecipient.Straße = org.Straße;
                myRecipient.PlzOrt = org.Plz.ToString().AddSpace() + org.Ort;
                myRecipient.EMail = org.EMail;
                myRecipient.Fax = org.FaxNr;
            }

            if (SelectedType == "Person") {
                myRecipient.Anrede = Resources.Anrede + (pers.Geschlecht == 2 ? " " + Resources.Frau : Resources.AnredeErgänzungHerr + Resources.Herr).AddSpace() + pers.Titel1.AddSpace() + pers.Zuname;
                if (this.chkZuHanden.Checked) {
                    myRecipient.Name = org.Prefix.AddSpace() + org.OrgName.AddSpace() + org.ZusätzeFW.AddSpace() + org.RechtsForm;
                    myRecipient.ZuHanden = (pers.Geschlecht == 1 ? Resources.Herr.AddSpace() : Resources.Frau.AddSpace()) + pers.Titel1.AddSpace() + pers.Vorname.AddSpace() + pers.Zuname;
                } else {
                    myRecipient.Name = pers.Titel1.AddSpace() + pers.Vorname.AddSpace() + pers.Zuname;
                }
                myRecipient.Straße = (pers.Straße == null | pers.Straße == string.Empty ? org.Straße : pers.Straße);
                myRecipient.PlzOrt = (pers.Plz == null | pers.Plz == string.Empty ? org.Plz.ToString() + " " + org.Ort : pers.Plz + " " + pers.Ort);
                myRecipient.EMail = (pers.eMail == null | pers.eMail == string.Empty ? org.EMail : pers.eMail);
                myRecipient.Fax = (pers.Fax == null | pers.Fax == string.Empty ? org.FaxNr : pers.Fax);
            }
            myRecipient.CheckZuHanden = this.chkZuHanden.Checked;
            myRecipient.Betreff = this.txtBetreff.Text;
        }

        private void SetDataBeforeClose() {
            SetRecipientData();
            Cancel = false;
        }

        #endregion

        #region Functions

        private string GetSelectedText() {
            string ret = "";
            StringBuilder txt = new StringBuilder();
            
            if (SelectedType == "Org") {
                DataGridView dgv = this.dgEmpfänger;
                DataGridViewRow row = dgv.CurrentRow;
                Organisation org = row.DataBoundItem as Organisation;    
                GlobalVar.currentOrganisation = org;
                txt.Append((org.Prefix ?? "").ToString() + " ");
                txt.Append((org.OrgName ?? "").ToString() + " ");
                if (this.rbMail.Checked) {
                    Versand = (int)GlobalVar.VersandArt.Mail;
                    txt.Append("(" + (org.EMail ?? "").ToString() + ")");
                }
                if (this.rbFax.Checked) {
                    Versand = (int)GlobalVar.VersandArt.Fax;
                    txt.Append("(" + (org.FaxNr ?? "").ToString() + ")");
                }
                if (this.rbBrief.Checked) {
                    Versand = (int)GlobalVar.VersandArt.Brief;
                    txt.Append("(" + (org.Plz + " " + org.Ort + ", " + org.Straße ?? "").ToString() + ")");
                }
            }
            if (SelectedType == "Person") {
                DataGridView dgv = this.dgPerson;
                DataGridView dOrg = this.dgEmpfänger;
                DataGridViewRow row = dgv.CurrentRow;
                DataGridViewRow rowOrg = dOrg.CurrentRow;

                Person pers = row.DataBoundItem as Person;
                Organisation org = rowOrg.DataBoundItem as Organisation;

                GlobalVar.currentPerson = pers;
                txt.Append((pers.Vorname ?? "").ToString() + " ");
                txt.Append((pers.Zuname ?? "").ToString() + " ");
                if (this.rbMail.Checked) {
                    Versand = (int)GlobalVar.VersandArt.Mail;
                    txt.Append("(" + (pers.eMail ?? "").ToString() + ") ");
                }
                if (this.rbFax.Checked) {
                    Versand = (int)GlobalVar.VersandArt.Fax;
                    txt.Append("(" + (this.chkZuHanden.Checked ? (pers.Fax == null | pers.Fax == string.Empty ? org.FaxNr : pers.Fax) : pers.Fax)  + ") ");
                }
                if (this.rbBrief.Checked) {
                    Versand = (int)GlobalVar.VersandArt.Brief;
                    txt.Append("(" + (this.chkZuHanden.Checked ? (org.Plz + " " + org.Ort + ", " + org.Straße ?? "").ToString() : (pers.Plz.AddSpace() + pers.Ort + ", " + pers.Straße ?? "").ToString()) + ") ");
                }
            }
            ret = txt.ToString();
            return ret;
        }

        #endregion

        #region Check - Events

        private void checkBox2_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox10_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox9_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e) {
            CheckBox chk = sender as CheckBox;
            SetBetreff(chk.Text, chk.Checked);
        }

        #endregion
               
    }
}
